package at.pervasive.mp3player.gui;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToggleButton;

import at.pervasive.mp3player.Mp3Player;
import at.pervasive.mp3player.Mp3PlayerListener;
import at.pervasive.mp3player.actions.AddFilesAction;
import at.pervasive.mp3player.actions.MuteUnmuteAction;
import at.pervasive.mp3player.actions.NextAction;
import at.pervasive.mp3player.actions.PlayPauseAction;
import at.pervasive.mp3player.actions.PreviousAction;
import at.pervasive.mp3player.actions.RemoveFilesAction;
import at.pervasive.mp3player.actions.ShuffleAction;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.FormSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.layout.Sizes;

/**
 * @author SHOCKIE
 */
public class Mp3PlayerFrame extends JFrame implements Mp3PlayerListener
{
	private final Mp3Player mp3Player;
	private PlaylistModel playlistModel;

	public Mp3PlayerFrame()
	{
		initComponents();

		playlistModel = new PlaylistModel();
		playList.setModel(playlistModel);
		playList.setCellRenderer(new FileRenderer());

		playList.addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent e)
			{
				if (e.getClickCount() == 2)
				{
					int selected = playList.getSelectedIndex();
					mp3Player.playFile(selected);
				}
			}
		});

		mp3Player = new Mp3Player();
		mp3Player.addMp3PlayerListener(this);

		muteButton.setAction(new MuteUnmuteAction(mp3Player));
		playPauseButton.setAction(new PlayPauseAction(mp3Player));
		nextButton.setAction(new NextAction(mp3Player));
		previousButton.setAction(new PreviousAction(mp3Player));
		addButton.setAction(new AddFilesAction(mp3Player));
		removeButton.setAction(new RemoveFilesAction(mp3Player, playList));
		shuffleButton.setAction(new ShuffleAction(mp3Player));
	}

	public Mp3Player getMp3Player()
	{
		return mp3Player;
	}

	private void initComponents()
	{
		// JFormDesigner - Component initialization - DO NOT MODIFY
		// //GEN-BEGIN:initComponents
		// Generated using JFormDesigner Open Source Project license - unknown
		JPanel controlPanel = new JPanel();
		previousButton = new JButton();
		playPauseButton = new JToggleButton();
		nextButton = new JButton();
		muteButton = new JToggleButton();
		JScrollPane playListScrollPane = new JScrollPane();
		playList = new JList();
		JPanel playListPanel = new JPanel();
		shuffleButton = new JButton();
		removeButton = new JButton();
		addButton = new JButton();
		CellConstraints cc = new CellConstraints();

		// ======== this ========
		setName("this");
		Container contentPane = getContentPane();
		contentPane.setLayout(new FormLayout(ColumnSpec.decodeSpecs("default:grow"), new RowSpec[] {
				FormFactory.DEFAULT_ROWSPEC, FormFactory.LINE_GAP_ROWSPEC,
				new RowSpec(RowSpec.CENTER, Sizes.DEFAULT, FormSpec.DEFAULT_GROW), FormFactory.LINE_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC }));

		// ======== controlPanel ========
		{
			controlPanel.setName("controlPanel");
			controlPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

			// ---- previousButton ----
			previousButton.setText("<<");
			previousButton.setName("previousButton");
			controlPanel.add(previousButton);

			// ---- playPauseButton ----
			playPauseButton.setText("> ||");
			playPauseButton.setName("playPauseButton");
			controlPanel.add(playPauseButton);

			// ---- nextButton ----
			nextButton.setText(">>");
			nextButton.setName("nextButton");
			controlPanel.add(nextButton);

			// ---- muteButton ----
			muteButton.setText("mute");
			muteButton.setName("muteButton");
			controlPanel.add(muteButton);
		}
		contentPane.add(controlPanel, cc.xywh(1, 1, 1, 1, CellConstraints.DEFAULT, CellConstraints.FILL));

		// ======== playListScrollPane ========
		{
			playListScrollPane.setName("playListScrollPane");

			// ---- playList ----
			playList.setName("playList");
			playListScrollPane.setViewportView(playList);
		}
		contentPane.add(playListScrollPane, cc.xywh(1, 3, 1, 1, CellConstraints.DEFAULT, CellConstraints.FILL));

		// ======== playListPanel ========
		{
			playListPanel.setName("playListPanel");
			playListPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

			// ---- shuffleButton ----
			shuffleButton.setText("shuffle");
			shuffleButton.setName("shuffleButton");
			playListPanel.add(shuffleButton);

			// ---- removeButton ----
			removeButton.setText("-");
			removeButton.setName("removeButton");
			playListPanel.add(removeButton);

			// ---- addButton ----
			addButton.setText("+");
			addButton.setName("addButton");
			playListPanel.add(addButton);
		}
		contentPane.add(playListPanel, cc.xywh(1, 5, 1, 1, CellConstraints.DEFAULT, CellConstraints.FILL));
		pack();
		setLocationRelativeTo(getOwner());
		// //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY
	// //GEN-BEGIN:variables
	// Generated using JFormDesigner Open Source Project license - unknown
	private JButton previousButton;
	private JToggleButton playPauseButton;
	private JButton nextButton;
	private JToggleButton muteButton;
	private JList playList;
	private JButton shuffleButton;
	private JButton removeButton;
	private JButton addButton;

	// JFormDesigner - End of variables declaration //GEN-END:variables
	@Override
	public void addedItems(List<File> files)
	{
		this.playlistModel.addFiles(files);
	}

	@Override
	public void muted()
	{
		this.muteButton.setSelected(true);
	}

	@Override
	public void paused()
	{
		this.playPauseButton.setSelected(true);
	}

	@Override
	public void playingNext()
	{
		int index = mp3Player.getCurrentIndex();
		this.playList.setSelectedIndex(index);
	}

	@Override
	public void playingPrevious()
	{
		int index = mp3Player.getCurrentIndex();
		this.playList.setSelectedIndex(index);
	}

	@Override
	public void removedItems(List<File> files)
	{
		this.playlistModel.removeFiles(files);
	}

	@Override
	public void shuffeled(List<File> mp3Files)
	{
		this.playlistModel.setFiles(mp3Files);
	}

	@Override
	public void startedPlaying(File file)
	{
		this.playPauseButton.setSelected(false);
		this.playList.setSelectedIndex(mp3Player.getCurrentIndex());
	}

	@Override
	public void unmuted()
	{
		this.muteButton.setSelected(false);
	}
}
